package com.example.front.service.impl;

import com.example.front.dao.ShippingDao;
import com.example.front.service.IShippingService;
import com.example.gok_mall.entity.Shipping;
import com.example.gok_mall.reponseData.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ShippingServiceImpl implements IShippingService {

    @Autowired
    private ShippingDao shippingDao;
    @Override
    /**
     * 购物车未选中状态
     */
    public ResultData add(Integer userId, Shipping shipping){
        shipping.setUserId(userId);
        shipping.setCreateTime(new Date());
        shipping.setUpdateTime(new Date());
        int rowCount=shippingDao.insertSelective(shipping);
        if(rowCount>=0){
            Map result= Maps.newHashMap();
            result.put("shippingId",shipping.getId());
            return ResultData.createData(result);
        }
        return ResultData.fail("新建地址失败");
    }

    //删除收货地址方法
    @Override
    public ResultData del(Integer userId,Integer shippingId){

        int rowCount=shippingDao.deleteByShippingIdByUserId(userId,shippingId);
        if(rowCount>0){
            return ResultData.success("删除地址成功");
        }
        return ResultData.fail("删除地址失败");

    }

    //修改地址方法
    @Override
    public ResultData update(Integer userId,Shipping shipping){
        shipping.setUserId(userId);
        Shipping selship=shippingDao.selectByShippingIdByUserId(userId,shipping.getId());
        if(selship == null){
            return ResultData.fail("该用户不存在此地址");
        }else {

        int rowCount= shippingDao.updateByshipping(shipping);
        if(rowCount>=0){
            Map result= Maps.newHashMap();
            result.put("shippingId",shipping.getId());
            return ResultData.createData(result);
        }
        }
        return ResultData.fail("更新地址失败");
    }

    //查询收货地址的方法
    @Override
    public  ResultData<Shipping> select(Integer userId,Integer shippingId){
        Shipping shipping=shippingDao.selectByShippingIdByUserId(userId,shippingId);
        if(shipping == null){
            return ResultData.fail("无法查询到该地址");
        }
        return ResultData.createData(shipping);
    }
    /**
     *     查询所有收货地址的方法
     */
    public ResultData<PageInfo> list(Integer userId,int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Shipping> shippingList=shippingDao.selectByUserId(userId);

        PageInfo pageInfo= new PageInfo(shippingList);
        return ResultData.createData(pageInfo);
    }

}
